package cc.eddic.pratice.programmingparadigms;


import cc.eddic.pratice.programmingparadigms.emp01array.EmployeeReaderService;
import cc.eddic.pratice.programmingparadigms.emp01array.impl.ReaderGcsImpl;
import cc.eddic.pratice.programmingparadigms.emp01array.impl.StatisticsLhImpl;
import cc.eddic.pratice.programmingparadigms.emp02list.EmployeeList;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;
import cc.eddic.pratice.programmingparadigms.emp02list.impl.EmployeeListGcsImpl;
import cc.eddic.pratice.programmingparadigms.emp02list.impl.EmployeeListLhImpl;
import cc.eddic.pratice.programmingparadigms.emp02list.impl.EmployeeListLxImpl;
import cc.eddic.pratice.programmingparadigms.emp02list.impl.StatisticsGcsImpl;
import cc.eddic.pratice.programmingparadigms.emp02list.EmployeeStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Slf4j
@Controller
@RequestMapping("/employee-list")
public class EmployeeListController {

    @RequestMapping("/stat-lh")
    // 谷常生实现的版本
//    @RequestMapping("/stat-gucs")
    public String statGcs(Model model, @RequestParam String[] jobTitleSort) {
        EmployeeReaderService readerService = new ReaderGcsImpl();
        EmployeeStatisticsService statisticsService =  new StatisticsGcsImpl();

        Employee[] employees = readerService.read("");
        EmployeeList employeeList = new EmployeeListLhImpl(employees);

        model.addAttribute("employees", employees);
        model.addAttribute("byGender", statisticsService.pivotByGender(employeeList).toArray());
        model.addAttribute("byDepartment", statisticsService.pivotByDepartment(employeeList).toArray());

        ResultList jobResultList = statisticsService.pivotByJobTitle(employeeList);
        if (log.isDebugEnabled()) log.debug("sort by {} {}", jobTitleSort[0], jobTitleSort[1]);
        final String field = jobTitleSort[0];
        final String order = jobTitleSort[1];
        if (!"key".equals(field) && !"count".equals(field)) {
            log.warn("invalid sort field {}", field);
        }
        if (!"asc".equals(order) && !"desc".equals(order)) {
            log.warn("invalid sort order {}", order);
        }
        jobResultList.sort(new ResultComparator() {
            @Override
            public int compare(Result r1, Result r2) {
                if ("key".equals(field)) {
                    if ("asc".equals(order)) {
                        return r1.getKey().compareTo(r2.getKey());
                    } else {
                        return r2.getKey().compareTo(r1.getKey());
                    }
                } else if ("count".equals(field)) {
                    if ("asc".equals(order)) {
                        return r1.getCount() - r2.getCount();
                    } else {
                        return r2.getCount() - r1.getCount();
                    }
                } else {
                    return r1.getCount() - r2.getCount();
                }
            }
        });
        model.addAttribute("byJobTitle", jobResultList.toArray());

        return THYMELEAF_STAT;
    }

    private final String THYMELEAF_STAT = "employee-statistics";
}
